<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:rx="http://songhaysystem.com/schemas/DataAccessConfiguration.xsd"
    >

    <xsl:output
        encoding="utf-8"
        indent="yes"
        media-type="text/xml"
        omit-xml-declaration="yes"
        standalone="no" />

    <xsl:param name="cmd" />

    <xsl:include href="./inc/App.xslt"/>
    <xsl:include href="./inc/Search.xslt"/>

    <xsl:template match="/">
        <div>
            <xsl:choose>
                <xsl:when test="//InternalMessage">
                    <xsl:call-template name="app_message" />
                </xsl:when>

                <xsl:when test="$cmd='CommandBlock'">
                    <xsl:call-template name="commands" />
                </xsl:when>
                <xsl:when test="$cmd='DataConnections'">
                    <xsl:call-template name="data_connections" />
                </xsl:when>
                <xsl:when test="$cmd='Search'">
                    <xsl:call-template name="search" />
                </xsl:when>
                <xsl:when test="$cmd='ListGenericWebIndexForEdit'">
                    <xsl:call-template name="tree" />
                </xsl:when>

                <xsl:otherwise>
                    <h1>Unknown Input</h1>
                    <p>
                        Command: <xsl:value-of select="$cmd"/>
                    </p>
                    <p>Server Response:</p>
                    <p>
                        <textarea id="RawServerResponse">
                            <xsl:copy-of select="/" />
                        </textarea>
                    </p>
                </xsl:otherwise>
            </xsl:choose>
        </div>
    </xsl:template>

    <xsl:template name="data_connections">
        <h1>Web Data Manager</h1>
        <h2>Available Webs</h2>
        <xsl:for-each select="//rx:DataAccessItem">
            <xsl:sort select="@DisplayName" />
            <p>
                <a href="{concat('./?cnn=',@Name)}">
                    <xsl:value-of select="@DisplayName" />
                </a>
            </p>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="commands">
        <form action="#" id="SearchForm">
            <div>
                <input id="SearchText" type="text" />
                <button id="Search">Go</button>
            </div>
        </form>

        <button id="Index">Index</button>
        <button id="NewDocument">Add New Document</button>
        <button id="NewFragment">Add New Fragment</button>
        <button id="NewSegment">Add New Segment</button>
        <hr />
        <button id="ChangeDataSource">Data Source</button>
    </xsl:template>

    <xsl:template name="search">

        <xsl:variable name="page_size" select="number(5)" />

        <h1>Search Results</h1>
        <p>
            <strong>Search String:</strong> <em>
                <xsl:value-of select="//SearchString[1]"/>
            </em>
            <br/>
            <strong>Segments Found:</strong> <em>
                <xsl:value-of select="count(//SearchResultGroup[GroupName = 'Segment'])" />
            </em>
            &#160;&#160;&#160;<strong>Documents Found:</strong> <em>
                <xsl:value-of select="count(//SearchResultGroup[GroupName = 'Document'])" />
            </em>
            &#160;&#160;&#160;<strong>Total:</strong> <em>
                <xsl:value-of select="count(//SearchResultGroup[GroupName = 'Document']) + count(//SearchResultGroup[GroupName = 'Segment'])" />
            </em>
        </p>
        <div>
            <table id="SearchResultsTable">
                <thead>
                    <xsl:if test="count(//SearchResultGroup) &gt; $page_size">
                        <tr>
                            <td class="Navigation">
                                <button id="SearchResultsPreviousRows" title="View Previous Set">◄</button>
                                <span id="NavigationCaption">&#160;</span>
                                <button id="SearchResultsNextRows" title="View Next Set">►</button>
                            </td>
                        </tr>
                    </xsl:if>
                    <tr>
                        <th>Search Results</th>
                    </tr>
                </thead>
                <xsl:for-each select="//SearchResultGroup">
                    <xsl:variable name="pos" select="position() - 1" />
                    <xsl:if test="((position() -1) mod $page_size) = 0">
                        <tbody>
                            <xsl:for-each select="//SearchResultGroup">
                                <xsl:if test="(position() &lt; ($pos + $page_size + 1)) and (position() &gt; $pos)">

                                    <xsl:variable name="class_name">
                                        <xsl:choose>
                                            <xsl:when test="string(./GroupName) = 'Document'">SearchResultForDocument</xsl:when>
                                            <xsl:when test="string(./GroupName) = 'Fragment'">SearchResultForFragment</xsl:when>
                                            <xsl:when test="string(./GroupName) = 'Segment'">SearchResultForSegment</xsl:when>
                                        </xsl:choose>
                                    </xsl:variable>

                                    <xsl:variable name="group_display_name">
                                        <xsl:choose>
                                            <xsl:when test="string(./GroupName) = 'Document'">Document</xsl:when>
                                            <xsl:when test="string(./GroupName) = 'Fragment'">Fragment</xsl:when>
                                            <xsl:when test="string(./GroupName) = 'Segment'">Segment</xsl:when>
                                        </xsl:choose>
                                    </xsl:variable>

                                    <tr>
                                        <td>
                                            <xsl:call-template name="get_search_item"/>
                                            <p>
                                                <a class="{$class_name}" id="{concat('item_',position(),'a_',./Identifier)}" title="{concat('Edit ',$group_display_name,' “',./DisplayIdentifier,'”')}">
                                                    Edit <xsl:value-of select="$group_display_name" /> [<xsl:value-of select="./Identifier" />]
                                                </a>
                                            </p>
                                        </td>
                                    </tr>
                                </xsl:if>
                            </xsl:for-each>
                        </tbody>
                    </xsl:if>
                </xsl:for-each>
            </table>
        </div>
    </xsl:template>

    <xsl:template name="tree">
        <div id="WebIndex">
            <h1 id="WebIndexHeader">Index</h1>
            <p id="IndexStatistics">
                <strong>Segments:</strong> <em>
                    <xsl:value-of select="//SegmentCount[1]" />
                </em>
                &#160;&#160;&#160;<strong>Documents:</strong> <em>
                    <xsl:value-of select="//DocumentCount[1]" />
                </em>
                &#160;&#160;&#160;<strong>Index Total:</strong> <em>
                    <xsl:value-of select="//IndexTotal[1]" />
                </em>
            </p>
            <h2 class="TopLevelHeader">Segments</h2>
            <ul class="SegmentLinkBlock">
                <xsl:for-each select="//Segment[not(ParentSegmentId)]">
                    <xsl:sort select="SegmentName" />
                    <li>
                        <div class="SegmentLinkBlock">
                            <a class="SegmentLink" href="#" id="{concat('item_s',position(),'_',./SegmentId)}">
                                <xsl:value-of select="./SegmentName" />
                            </a>
                        </div>

                        <xsl:apply-templates select="." />

                    </li>
                </xsl:for-each>
            </ul>
        </div>
    </xsl:template>

    <xsl:template match="Segment">

        <xsl:variable name="id" select="./SegmentId" />

        <xsl:if test="count(//SegmentDocuments/SegmentDocument[SegmentId=$id]) &gt; 0">
            <div>
                <xsl:variable name="anchor_suffix" select="concat(generate-id(.),position())" />
                <xsl:if test="position() = 1">
                    <h2>
                        <a class="WebIndexTog" href="#" id="{concat('WebIndexTogAnchor',$anchor_suffix)}">&#160;</a>“<xsl:value-of select="./SegmentName" />” Documents
                    </h2>
                </xsl:if>
                <ul class="DocumentLinkBlock" id="{concat('WebIndexBlock',$anchor_suffix)}">
                    <xsl:for-each select="//SegmentDocuments/SegmentDocument[SegmentId=$id]">
                        <xsl:sort select="./Title" />
                        <li>
                            <div class="DocumentLinkBlock">
                                <xsl:variable name="cssclass_doclink">
                                    <xsl:choose>
                                        <xsl:when test="number(./IsActive) = 0">DocumentLink_Inactive</xsl:when>
                                        <xsl:otherwise>DocumentLink</xsl:otherwise>
                                    </xsl:choose>
                                </xsl:variable>
                                <a class="{$cssclass_doclink}" href="#" id="{concat('item_d',position(),'_',./DocumentId)}">
                                    <xsl:value-of select="./Title" />
                                    <xsl:if test="(string-length(./DocumentShortName) &gt; 0) and (string-length(./FileName) &gt; 0)"><br/>[<xsl:value-of select="./DocumentShortName" />, <xsl:value-of select="./FileName" />]</xsl:if>
                                </a>
                            </div>
                        </li>
                    </xsl:for-each>
                </ul>
            </div>
        </xsl:if>

        <xsl:if test="count(//Segment[ParentSegmentId=$id]) &gt; 0">
            <div>
                <xsl:variable name="anchor_suffix" select="concat(generate-id(.),position()+1)" />
                <xsl:if test="position() = 1">
                    <h2>
                        <a class="WebIndexTog" href="#" id="{concat('WebIndexTogAnchor',$anchor_suffix)}">&#160;</a>“<xsl:value-of select="./SegmentName"/>” Sub-Segments
                    </h2>
                </xsl:if>
                <ul class="SegmentLinkBlock" id="{concat('WebIndexBlock',$anchor_suffix)}">
                    <xsl:for-each select="/Segments/Segment[ParentSegmentId=$id]">
                        <xsl:sort select="./SegmentName" />
                        <li>
                            <div class="SegmentLinkBlock">
                                <xsl:variable name="cssclass_seglink">
                                    <xsl:choose>
                                        <xsl:when test="number(./IsActive) = 0">SegmentLink_Inactive</xsl:when>
                                        <xsl:otherwise>SegmentLink</xsl:otherwise>
                                    </xsl:choose>
                                </xsl:variable>
                                <a class="{$cssclass_seglink}" href="#" id="{concat('item_s',position(),'_',./SegmentId)}">
                                    <xsl:value-of select="./SegmentName" />
                                </a>
                            </div>

                            <xsl:apply-templates select="." />

                        </li>

                    </xsl:for-each>
                </ul>
            </div>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>
